<!--
  移动签批->公文详情->发电->修改公文要素
-->
<template>
	<view>
		<van-popup v-model="showCompo" closeable round position="bottom" :style="{ height: '100%' }"
			@click-close-icon="toClose" @click-overlay="toClose">
			<van-row type="flex" justify="center">
				<van-col offset="1" span="8">
					<van-cell value="修改公文要素" />
				</van-col>
			</van-row>
			<van-form>
				<van-collapse v-model="tableShow" accordion :border="border">
					<van-collapse-item icon="label-o" title="公文要素" title-class="text-bold" name="jbxx">
            <van-field ref="title" v-model="formData.title" required label="标题" placeholder="请输入" input-align="right" autosize rows="1" type="textarea" />

						<van-cell is-link title="密级" :value="this.dict.getDictNameByDictCode('mj',formData.secrecyLevel)" @click="show = true" />
						<van-action-sheet v-model="show" :actions="mj" cancel-text="取消" @select="onSelect" />

						<van-cell is-link title="缓急" :value="this.dict.getDictNameByDictCode('gwfdjjcd',formData.emergencyLevel)" @click="show1 = true" />
						<van-action-sheet v-model="show1" :actions="hj" cancel-text="取消" @select="onSelect1" />

						<van-field label="拟稿日期" v-if="formData.createTime" :value="formData.createTime.substring(0,10)" @click="showCalendar = true" placeholder="请选择" input-align="right" is-link/>
						<van-calendar v-model:showCalendar="showCalendar" :show-confirm="false" @confirm="onConfirm" />

            <van-field v-model="formData.eachPage" label="页数" placeholder="请输入" input-align="right" />

						<van-cell  title="落款单位" :value="formData.signDeptName"  is-link @click="showOrgTreeSheet('1')"/>
						<orgTree :showOrgtree="showOrgtreeLK" :nodeData="nodeDataDrafter" :drafterDeptData="drafterSignDeptData" @closeOrgTreeSheet="closeOrgTreeSheet"  ></orgTree>

						<van-cell ref="drafterDeptName" required title="承办单位"  :value="formData.drafterDeptName"  is-link @click="showOrgTreeSheet('2')"/>
						<orgTree2 :showOrgtree="showOrgtreeCB" :nodeData="nodeDataDrafter" :drafterDeptData="drafterDeptData" @closeOrgTreeSheet="closeOrgTreeSheet"  ></orgTree2>

            <van-cell ref="name" title="拟稿" :value="formData.drafterName"    @click="showDrafterSheet"  is-link/>
            <drafter :showDrafter="showDrafter" :nodeData="nodeDataDrafter" :drafterData="drafterData"
                     @closeDrafterSheet="closeDrafterSheet"></drafter>

            <van-field ref="phone" v-model="formData.drafterPhone" required label="联系电话" type="tel" placeholder="请输入" input-align="right"/>

            <van-cell ref="public" required is-link title="是否公开" :value="this.dict.getDictNameByDictCode('infopublic',formData.infoDisclosure)" @click="show3 = true" />
            <van-action-sheet v-model="show3" :actions="sfgk" cancel-text="取消" @select="onSelect3" />
            <van-field v-if="formData.infoDisclosure==1" v-model="formData.nopublicReason" label="不公开理由" placeholder="请输入" input-align="right" />

            <van-cell  title="会签单位" :value="formData.countersign"  is-link @click="showSignOrgSheet()"/>
            <signOrg :showSignOrg="showSignOrg" :checkedVal="formData.countersign" :fwId="formData.extendIdForRelate" @closeSignOrgSheet="closeSignOrgSheet"  ></signOrg>

            <van-field v-model="formData.description" label="拟稿说明" placeholder="请输入" input-align="right" />
					</van-collapse-item>
					<van-collapse-item icon="font-o" title="已附加公文" title-class="text-bold" name="zwjfj">
						<!-- <van-cell title="公文" v-for="(item,index) in relation" :value="item.title" value-class="text-sm text-blue" /> -->
						<view class="opinion" v-if="relation.length>0 ">
							<text class="opinion-left">附加公文</text>
							<view class="opinion-right">
								<text  v-for="(item,index) in relation">{{item.title}}</text>
							</view>
						</view>
					</van-collapse-item>
				</van-collapse>
			</van-form>
			<!-- <van-popup v-model="showPopup" closeable round position="bottom" :style="{ height: '30%' }">
				<van-action-sheet />
			</van-popup> -->


			<view class="padding-top-lg padding-bottom-lg">
				<van-row type="flex" justify="space-around">
					<van-col span="10">
						<van-button type="info" round plain hairline block @click="toClose">取消</van-button>
					</van-col>
					<van-col span="10">
						<van-button type="info" round block @click="toEdit">确定</van-button>
					</van-col>
				</van-row>
			</view>
		</van-popup>
	</view>
</template>

<script>
	import forward from 'pages/admin/components/sendMsgDoc/compo-forward';
	import orgTree from 'pages/admin/components/sendMsgDoc/compo-orgtree';//单位选择组件
	import orgTree2 from 'pages/admin/components/sendMsgDoc/compo-orgtree';//单位选择组件
	import signOrg from 'pages/admin/components/sendMsgDoc/compo-signorg';//会签单位选择组件
	import drafter from 'pages/admin/components/common/compo-drafter';//拟稿人选择
	export default {
		components: {
			forward,
			orgTree,
			orgTree2,
			signOrg,
			drafter
		},
		props: {
			showEdit: {
				type: Boolean,
				default: false
			},
		},
		watch: {
			filterText(val) {
				this.$refs.tree.filter(val);
			},
			showEdit(val){
				this.showCompo = val
			},

		},
		data() {
			return {
				showCompo:false,//组件显示
				showCalendar:false,//日历组件显示
				//1主送 3抄送 4分送
				forwardState1:false,//分送
				forwardState3:false,//抄送
				forwardState4:false,//分送
				showOrgtreeLK:false,//落款单位选择
				showOrgtreeCB:false,//承办单位选择
				showSignOrg:false,//会签单位选择
				showDrafter:false,//拟稿人组件
				//拟稿人选择，查询单位所需节点和隐藏节点
				nodeDataDrafter:{
					showNode: '002001', //显示的节点
					hideNode: '002007031,002001006,002002002,002003004,002004002,002005002,002006029', //隐藏的节点
				},
				//拟稿人信息，串回子组件回显用
				drafterData:{
					userName:'',
					userId:''
				},
        //承办单位信息回显用
        drafterDeptData:{
          drafterDeptId:'',
          drafterDeptName:''
        },
        //落款单位
        drafterSignDeptData:{
          drafterDeptId:'',
          drafterDeptName:''
        },
				countersign:[],//会签单位名称数组
				countersignValue:[],//会签单位值数组
				relation:[],//附加公文列表
				filterText: '',
				expandKeys: ["0"],//默认展开的节点-对应的deptId的值
				props: function() {
					return {
						// 这里的label就可以使用函数进行自定义的渲染了
						// label(data, node) {
						// 	return '节点' + node.data.personName;
						// },
						label: 'deptName', // 指把数据中的‘personName’当做label也就是节点名称
						children: 'children' // 指把数据中的‘childs’当做children当做子节点数据
					}
				},
				ready: false, // 这里用于自主控制loading加载状态，避免异步正在加载数据的空档显示“暂无数据”
				showOrg:false,
				formData:{},//表单数据
				showPopup:false,
				border: false,
				show: false,
				show1: false,
				show2: false,
				show3: false,
				show4: false,
				show5: false,
				show6: false,
				show7: false,
				show8: false,
        bgqx:[],//保管期限
				topTitle: 'gwxq',
				tableShow: "jbxx",
				bjsj: '',
				password: '',
				to:'',
				cc:'',
				finishTime:'',
				bt: '',
				zs: '',
				zpsfys: '',
				bgkly: '',
				xzhqdw: '',
				ngsm: '',
				ng: '',
				lxdh: '',
				zwjfj: '',
				xgcl: '',
				gw1: '',
				gw2: '',
				mj: [{
					name: '无'
				}, {
					name: '普通加密'
				}, {
					name: '特殊加密'
				}],
				hj: [{
					name: '平件1'
				}, {
					name: '急件1'
				}, {
					name: '特急1'
				}],
				xzxklgw: [{
					name: '是'
				}, {
					name: '否'
				}],
				sfgk: [{
					name: '不公开'
				}, {
					name: '主动公开'
				}, {
					name: '依申请公开'
				}],
				wjsx: [{
					name: '非行政规范性文件'
				}, {
					name: '一般行政规范性文件'
				}, {
					name: '重要行政规范性文件'
				}],
				sfwzyzcwj: [{
					name: '是'
				}, {
					name: '否'
				}],
				sfyjjfa: [{
					name: '是'
				}, {
					name: '否'
				}],
				sfwywjhfxshhjg: [{
					name: '是'
				}, {
					name: '否'
				}],
				sfdcfkgw: [{
					name: '是'
				}, {
					name: '否'
				}],
				sfsdcsx: [{
					name: '是'
				}, {
					name: '否'
				}],
				mjT: "无",
				hjT: "平件",
				xzxklgwT: "否",
				sfgkT: "不公开",
				wjsxT: "请选择",
				sfwzyzcwjT: "否",
				sfyjjfaT: "否",
				sfwywjhfxshhjgT: "否",
				sfsdcsxT: "否",
				title:'',
				author:'',
				digit:'',
				message:'',
				countersignDept:'',
				countersignDeptId:'',
				reason:'',
			};
		},
		mounted() {
			this.getDict()//获取字典项
			this.getShowDept()
		},

		methods: {
			//拟稿人
			showDrafterSheet(){
				this.showDrafter = true
			},
			//关闭拟稿人组件
			closeDrafterSheet(userInfo){
				//userInfo: {"userId":"4926","userName":"高萌"}
				this.formData.drafterId = userInfo.userId
				this.formData.drafterName = userInfo.userName
				this.showDrafter = false
			},
			onConfirm(date){
				const formatDate = (date) => `${date.getFullYear()}-${date.getMonth() + 1 >= 10 ? (date.getMonth() + 1) : '0' +(date.getMonth() + 1)}-${date.getDate() >= 10 ? date.getDate() : '0' + date.getDate()} ${date.getHours() >= 10 ? date.getHours():'0' + date.getHours()}:${date.getMinutes()>=10?date.getMinutes():'0'+date.getMinutes()}:${date.getSeconds() >= 10 ? date.getSeconds():'0' + date.getSeconds()} `
				this.showCalendar = false
				this.formData.createTime=formatDate(date)
			},
			//类型 1主送 3抄送 4分送
			showforward(tagType){
				if(tagType ==1 ){
					this.forwardState1 = true
				}else if(tagType ==3){
					this.forwardState3 = true
				}else if(tagType ==4){
					this.forwardState4 = true
				}

			},

			//子组件通知父组件关闭
			closeSheet(data){
				 //1主送 3抄送 4分送
				console.log("tagType: " + JSON.stringify(data));
				if(data.tagType ==1 ){
					if(data.tagId != ''){
						this.formData.mainTo = data.content
					}
					this.forwardState1 = false
				}else if(data.tagType ==3){
					if(data.tagId != ''){
						this.formData.copyTo = data.content
					}
					this.forwardState3 = false
				}else if(data.tagType ==4){
					if(data.tagId != ''){
						this.formData.copyReports = data.content
					}
					this.forwardState4 = false
				}

			},

			//点击会签单位
			showSignOrgSheet(){
				this.showSignOrg = true
			},
			//落款单位选择type=1,落款单位选择，type=2，承办单位选择
			showOrgTreeSheet(type){
				if(type == 1){
					this.showOrgtreeLK = true
				}else if(type == 2){
					this.showOrgtreeCB = true
				}else{
					console.log('页面出错了');
				}

			},
			//子组件通知父组件关闭单位选择
			closeOrgTreeSheet(data){
				//data {"orgName":"航安办","deptCode":"002001007"}
				if(this.showOrgtreeCB){
					this.showOrgtreeCB = false
          this.formData.drafterDeptName = data.orgName//承办单位名称
          this.formData.drafterDeptId = data.deptCode//承办单位ID
          // console.log("单位选择data: " + JSON.stringify(data));
				}
				if(this.showOrgtreeLK){
					this.showOrgtreeLK = false
          this.formData.signDeptName = data.orgName//落款单位名称
          this.formData.signDept = data.deptCode//落款单位ID
          console.log("单位选择data: " + JSON.stringify(data));
				}

			},
			//子组件通知父组件关闭机构树
			closeSignOrgSheet(data){
				// data:["办公室","航安办"]
				console.log("org ---- data: " + JSON.stringify(data));
				this.countersign=[]//置空，以防多次追加
				this.formData.countersign=data.toString()//给表单赋值，提交表单用
				this.showSignOrg = false
			},
			//密级
			onSelect(item) {
				//item: {"name":"无","dictItemCode":"1"}
				// 默认情况下点击选项时不会自动收起
				// 可以通过 close-on-click-action 属性开启自动收起
				this.formData.secrecyLevel=item.dictItemCode
				// this.formData.secrecyLevelName = item.name
				this.show = false;
			},
			//缓急
			onSelect1(item) {
				this.formData.emergencyLevel=item.dictItemCode
				// this.formData.emergencyLevelName = item.name
				this.show1 = false;
			},
			//行政许可类公文
			onSelect2(item) {
			this.formData.xzsplgw=item.dictItemCode
			// this.formData.xzsplgwName = item.name
				this.show2 = false;
			},
			//是否公开
			onSelect3(item) {
				this.formData.infoDisclosure=item.dictItemCode
				// this.formData.infoDisclosureName = item.name
				this.show3 = false;
			},
			//文件属性
			onSelect4(item) {
				this.formData.fileAttribute=item.dictItemCode
				// this.formData.fileAttributeName = item.name
				this.show4 = false;
			},
			//是否为重要政策性文件
			onSelect5(item) {
				this.formData.isPolicy=item.dictItemCode
				// this.formData.isPolicyName = item.name
				this.show5 = false;
			},
			//是否有解读方案
			onSelect6(item) {
				this.formData.isInterpret=item.dictItemCode
				// this.formData.isInterpretName = item.name
				this.show6 = false;
			},
			//是否有文件合法性审查结果
			onSelect7(item) {
				this.formData.isLegal=item.dictItemCode
				// this.formData.isLegalName = item.name
				this.show7 = false;
			},
      onSelect8(item) {
        this.formData.storagePeriod=item.dictItemCode
        // this.formData.isSuperviseName = item.name
        this.show8 = false;
      },
			//取消按钮
			toClose(){
				this.$emit('closeEdit', this.showEdit);
			},
			//确定按钮
			toEdit(){
				this.saveelement()
			},
			// 表单校验
			  validateForm() {
				if(this.util.isEmpty(this.formData.title)) {
				  this.$refs.title.focus()
				  this.$toast('请填写标题')
				  return false
				}
          if (this.util.isEmpty(this.formData.drafterDeptName)) {
            this.$refs.drafterDeptName.focus()
            this.$toast('请选择承办单位')
            return false
          }
				if (this.util.isEmpty(this.formData.infoDisclosure)) {
				  this.$refs.public.focus()
				  this.$toast('请选择是否公开')
				  return false
				}
				if (this.util.isEmpty(this.formData.drafterName)) {
				  this.$refs.name.focus()
				  this.$toast('请填写拟稿人')
				  return false
				}
				if (this.util.isEmpty(this.formData.drafterPhone)) {
				  this.$refs.phone.focus()
				  this.$toast('请填写联系电话')
				  return false
				}
				return true;
			  },
			//修改公文要素
			saveelement(){
				console.log("修改公文要素==>: " + JSON.stringify(this.formData));
				var _this = this
				if(_this.validateForm()) {
					this.$httploading({
					  //修改呈批件接口和发文发电不一样
					  url: this.$http.adornUrl(`/caac/oa/doc/hbglj/gwfd/saveelement?systemKey=`+this.cache.get('userInfo').systemKey+`&systemFlag=`+this.cache.get('userInfo').systemFlag),
					  method: 'post',
					  data:JSON.stringify(this.formData)

					}).then(({data}) => {
						console.log("修改发电公文要素data: " + JSON.stringify(data));
						if(data.code==200){
							_this.$toast('修改公文要素成功')
							_this.$emit('closeEdit', this.showEdit);
							_this.$emit('refreshBtnGroup',true)//修改成功后通知父组件重新加载数据
							// console.log("_this.formData: " + JSON.stringify(_this.formData));
						}
					})
				}
			},
			//父组件点击修改公文要素时调用获取详情回显到表单
			editGw(proInstId){

				console.log("获取公文要素详情"+proInstId);
				var _this = this
				this.$httploading({
				  url: this.$http.adornUrl(`/caac/oa/doc/hbglj/gwfd/proInstId/`+proInstId),
				  method: 'get',
				  params: this.$http.adornParams({
					  'systemKey': this.cache.get('userInfo').systemKey,//固定值
					  'systemFlag': this.cache.get('userInfo').systemFlag//固定值
					  // 'systemKey': 'ls_liu',//固定值
					  // 'systemFlag': 'yidong'//固定值
				  })
				}).then(({data}) => {
					console.log("获取公文要素详情data: " + JSON.stringify(data));
					if(data.code==200){
						_this.formData = data.data
						this.drafterData.userId = _this.formData.drafterId
						this.drafterData.userName = _this.formData.drafterName

            //承办单位
            this.drafterDeptData.drafterDeptId = _this.formData.drafterDeptId
            this.drafterDeptData.drafterDeptName = _this.formData.drafterDeptName

            //落款单位
            this.drafterSignDeptData.drafterDeptId = _this.formData.signDept
            this.drafterSignDeptData.drafterDeptName = _this.formData.signDeptName

						_this.getRelation(data.data.extendIdForRelate)
					}
				})
			},
			//获取附加公文
			getRelation(extendIdForRelate){
				var _this = this
				this.$http({
				  url: this.$http.adornUrl(`/caac/oa/mhjComponent/relationfile/relation`),
				  method: 'get',
				  params: this.$http.adornParams({
					  'docId': extendIdForRelate,
					  'systemKey': this.cache.get('userInfo').systemKey,//固定值
					  'systemFlag': this.cache.get('userInfo').systemFlag//固定值
				  })
				}).then(({data}) => {
					//[{"id":"1c0962f8839d46f792f84e5c89c96f14","title":"ppap","relateDocId":"4ec205b8334446f9b932d0fcb3896bc8","proInstId":"4ec205b8334446f9b932d0fcb3896bc8"},{"id":"2f4aa437ecba4c748bff911f03667147","title":"py","relateDocId":"86d30c881c494b468534685bb0746174","proInstId":"86d30c881c494b468534685bb0746174"}]
					if(data.code==200){
						if(data.data != null){
							_this.relation =data.data
						}
					}
					console.log("附加公文: " + JSON.stringify(data));
				})
			},
			//获取要显示的组织机构代码
			getShowDept(){
				this.$http({
				  url: this.$http.adornUrl(`/caac/oa/authority/gljfw/getShowDept/`+this.cache.get('userInfo').deptId),
				  method: 'get',
				  params: this.$http.adornParams({
					  'systemKey': this.cache.get('userInfo').systemKey,//固定值
					  'systemFlag': this.cache.get('userInfo').systemFlag//固定值
				  })
				}).then(({data}) => {
					if(data.code==200){
						if(data.data != null){
							this.nodeDataDrafter.showNode = data.data.hideDepts
							this.nodeDataDrafter.hideNode = ''//要隐藏的节点
						}
					}
				})
			},
			//获取需要的字典项
			getDict(){
				const dictSecrecyLevel = this.cache.get('mj')//密级
				//给van-action-sheet 赋值
				this.mj=[]
				dictSecrecyLevel.forEach((item)=>{
					this.mj.push({name:item.dictItemName,dictItemCode:item.dictItemCode})
				})

        const dictEmergencyLevel = this.cache.get('gwfdjjcd')//紧急程度
				this.hj=[]
				dictEmergencyLevel.forEach((item)=>{
					this.hj.push({name:item.dictItemName,dictItemCode:item.dictItemCode})
				})

        const dictXzsplgw = this.cache.get('xzsplgw')//紧急程度
				this.xzxklgw=[]
				dictXzsplgw.forEach((item)=>{
					this.xzxklgw.push({name:item.dictItemName,dictItemCode:item.dictItemCode})
				})

        const dictInfoDisclosure = this.cache.get('infopublic')
				this.sfgk=[]
				dictInfoDisclosure.forEach((item)=>{
					this.sfgk.push({name:item.dictItemName,dictItemCode:item.dictItemCode})
				})

        const dictFileAttribute = this.cache.get('fileAttribute')//文件属性
				this.wjsx=[]
				dictFileAttribute.forEach((item)=>{
					this.wjsx.push({name:item.dictItemName,dictItemCode:item.dictItemCode})
				})

        const dictIsPolicy = this.cache.get('isPolicy')//是否为重要政策性文件
				this.sfwzyzcwj=[]
				dictIsPolicy.forEach((item)=>{
					this.sfwzyzcwj.push({name:item.dictItemName,dictItemCode:item.dictItemCode})
				})

        const dictIsInterpret = this.cache.get('isInterpret')//是否有解决方案
				this.sfyjjfa=[]
				dictIsInterpret.forEach((item)=>{
					this.sfyjjfa.push({name:item.dictItemName,dictItemCode:item.dictItemCode})
				})

        const dictIsLegal = this.cache.get('isLegal')//是否有文件合法性审查结果
				this.sfwywjhfxshhjg=[]
				dictIsLegal.forEach((item)=>{
					this.sfwywjhfxshhjg.push({name:item.dictItemName,dictItemCode:item.dictItemCode})
				})

        //保管期限
        const dictBgqx = this.cache.get('bgqx')//保管期限
        this.bgqx=[]
        dictBgqx.forEach((item)=>{
          this.bgqx.push({name:item.dictItemName,dictItemCode:item.dictItemCode})
        })

        const dictIsSupervise = this.cache.get('isSupervise')//是否有文件合法性审查结果
				this.sfdcfkgw=[]
        dictIsSupervise.forEach((item)=>{
					this.sfdcfkgw.push({name:item.dictItemName,dictItemCode:item.dictItemCode})
				})
			},


		},


	};
</script>

<style scoped>
  /* 意见相关样式 */
  .opinion{
    display: flex;
    flex-direction: column;
    margin: 0 32rpx;
    padding: 20rpx 0rpx;
    overflow: hidden;
    font-size: 28rpx;
    line-height: 24px;
    border-bottom: 1px solid #ebedf0;

  }
  .opinion .opinion-left{
    text-align: left;
    color: #323233;
  }
  .opinion .opinion-right{
    text-align: right;
    color: #969799;
  }
	.van-cell__title {
		min-width: 55%;

		span {
			display: inline-block;
			text-align: left;
			word-break: break-all;
		}
	}

	/deep/ .van-collapse-item__content {
		padding: 0px;
		color: #969799;
		font-size: 14px;
		line-height: 1.5;
		background-color: #fff;
	}

	/deep/ .van-field__label {
		color: #323233;
		min-width: 50%;

		span {
			display: inline-block;
			text-align: left;
			word-break: break-all;
		}
	}

	/deep/ .van-field__control {
		font-size: 14px;
		color: #969799;
	}
</style>
